package com.suning.sawp.in.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.EmployeeInfo;
import com.suning.sawp.dto.region.RegionDto;
import com.suning.sawp.in.filter.RequestUserHolder;
import com.suning.sawp.intf.store.StoreInfoService;

@Controller
@RequestMapping("/adminPage/region")
public class RegionController extends DjBaseController{
    
    @Autowired
    StoreInfoService storeInfoService;
    
    @RequestMapping("/queryAllRegion.do")
    public MyGsonView queryAllRegion(){
        EmployeeInfo employeeInfo = RequestUserHolder.getRequestUser();
        MyGsonView result = createGsonView();
        List<RegionDto> regions = null;
        if (null != employeeInfo && 
                StringUtils.isNotBlank(employeeInfo.getRegionCode())) {
            regions = new ArrayList<RegionDto>();
            regions.add(new RegionDto(employeeInfo.getRegionCode(), 
                    employeeInfo.getRegionName()));
        } else {
            regions = storeInfoService.queryAllRegions();
        }
        
        result.addStaticAttribute("regions", regions);
        return result;
    }
    
    @RequestMapping("/queryStroeByRegionCode.do")
    public MyGsonView queryStroeByRegionCode(String regionCode){
        
        MyGsonView result = createGsonView();
        if(StringUtils.isBlank(regionCode)){
            setErrorMsg(result, ErrorCodeConstants.ERR_0005);
            return result;
        }
        result.addStaticAttribute("storeInfos", storeInfoService.queryStoresByRegionCode(regionCode));
        return result;
    }
    
}
